package com.venus.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.venus.entity.Page;
import com.venus.entity.User;
import com.venus.mapper.BaseMapper;
import com.venus.service.BaseService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by admin on 13/10/17.
 */
@Service
public class BaseServiceImpl implements BaseService{

	@Resource
	private BaseMapper baseMapper;

	@Override
	public User findByUsername(String username) {
		return baseMapper.findByUsername(username);
	}

	@Override
	public Map<String, String> loginValidate(User user) {
		Map<String,String> result = new HashMap<>();
		String massage = "";
		UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(),user.getPassword());
		token.setRememberMe(true);
		Subject subject = SecurityUtils.getSubject();
		try {
			subject.login(token);
			if (subject.isAuthenticated()) {
				result.put("result","success");
				result.put("loginUser",user.getUsername());
				return result;
			} else {
				result.put("result","invalid");
				return result;
			}
		} catch (IncorrectCredentialsException e) {
			massage = "登录密码错误. Password for account " + token.getPrincipal() + " was incorrect.";
			result.put("result","invalid");
			result.put("message",massage);
			return result;
		} catch (ExcessiveAttemptsException e) {
			massage = "登录失败次数过多";
			result.put("result","invalid");
			result.put("message",massage);
			return result;
		} catch (LockedAccountException e) {
			massage = "帐号已被锁定. The account for username " + token.getPrincipal() + " was locked.";
			result.put("result","invalid");
			result.put("message",massage);
			return result;
		} catch (DisabledAccountException e) {
			massage = "帐号已被禁用. The account for username " + token.getPrincipal() + " was disabled.";
			result.put("result","invalid");
			result.put("message",massage);
			return result;
		} catch (ExpiredCredentialsException e) {
			massage = "帐号已过期. the account for username " + token.getPrincipal() + "  was expired.";
			result.put("result","invalid");
			result.put("message",massage);
			return result;
		} catch (UnknownAccountException e) {
			massage = "帐号不存在. There is no user with username of " + token.getPrincipal();
			result.put("result","invalid");
			result.put("message",massage);
			return result;
		} catch (UnauthorizedException e) {
			massage = "您没有得到相应的授权！" + e.getMessage();
			result.put("result","invalid");
			result.put("message",massage);
			return result;
		}
	}

	@Override
	public Set<String> findPermissionsByUserId(long userId) {
		return baseMapper.findPermissionsByUserId(userId);
	}

	@Override
	public Set<String> findByRolesUserId(long userId) {
		return baseMapper.findByRolesUserId(userId);
	}

	@Override
	public PageInfo<User> findUsersByPageList(Page page) {
		PageHelper.startPage(page.getCurrentPage(), page.getSize());
		List<User> list = baseMapper.findUsers((User)page.getObject());
		return new PageInfo<User>(list);
	}
}
